
// @ts-nocheck
export const chooseLocationMP = (callback) => {
    const _this = this;
    uni.getSetting({
        success(res) {
            // console.log(res, '***')
            if (res.authSetting['scope.userLocation'] != undefined && res.authSetting[
                'scope.userLocation'] != true) {
                uni.showModal({
                    title: '是否授权当前位置',
                    content: '你的位置信息将用于应用选择地点，请确认授权',
                    showCancel: false,
                    success: function (res) {
                        if (res.cancel) {
                            uni.showToast({
                                title: '取消授权',
                                icon: 'loading',
                                duration: 2000
                            });
                            return;
                        }
                        uni.openSetting({
                            success: function (data) {
                                if (data.authSetting["scope.userLocation"] !==
                                    true) {
                                    uni.showToast({
                                        title: '授权失败',
                                        icon: 'loading',
                                        duration: 2000
                                    });
                                    return;
                                }
                                // uni.showToast('授权成功', 2000, 'success');
                                uni.showToast({
                                    title: '授权成功',
                                    icon: 'success',
                                    duration: 2000
                                });
                            }
                        })
                    }
                })
            } else {
                uni.authorize({
                    scope: 'scope.userLocation',
                    success(res) {
                        uni.chooseLocation({
                            success: (res) => {
                                // console.log(res, 111);
                                callback(res)
                            },
                            fail: () => {
                                uni.showToast({
                                    title: '获取位置失败',
                                    icon: 'loading',
                                    duration: 2000
                                })
                            }
                        });
                    },
                    fail(err) {
                        uni.showToast({
                            title: '拒绝授权位置,将无法选择位置信息',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                })


            }
        }
    });
}

export const getAddress = (callback) => {
    var that = this;
    var weidu = '';
    var jd = '';
    uni.getLocation({
        geocode: true,
        type: 'wgs84',
        success: (res) => {
            // console.log('位置是', res)
            weidu = res.latitude
            jd = res.longitude
            uni.request({
                url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + weidu +
                    ',' + jd + '&key=RFZBZ-23GWK-WGFJ7-ABZRB-F4FMF-YCFWN&get_poi=0',
                success: function (res) {
                    callback(res)
                }
            })
        },
        fail: (res) => {
            // console.log(res);
            // console.log(res, "经纬度没有授权")
            uni.getSetting({
                success: (res) => {
                    let authSetting = res.authSetting;
                    if (!res.authSetting['scope.userLocation']) {
                        uni.showModal({
                            title: '您未开启地理位置授权',
                            content: '小程序将无法正常使用',
                            success: (res) => {
                                if (res.confirm) {
                                    uni.openSetting()
                                }
                            }
                        })
                    } else {
                        uni.showModal({
                            title: '您未开启手机地理位置',
                            content: '小程序将无法正常使用',
                            success: (res) => {
                                if (res.confirm) {
                                    uni.openSetting()
                                }
                            }
                        })
                    }
                }
            })
        }
    })
}
